package com.ocom.webapi.controller;

import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.entity.human.EntityPersonTag;
import com.ocom.common.entity.human.EntityPersonTagType;
import com.ocom.common.request.human.RemovePerTagGroupRequest;
import com.ocom.common.request.ugcapi.PersonTagTypeRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.HumanClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/7/7
 **/
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class PersonTagController {

	@Autowired
	HumanClientService humanClientService;

	@PostMapping(WebApiServiceUrl.Url_PerTagOnShow)
	public Result PerTagOnShow() {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		Result result = humanClientService.getPerTag(yoCiUser.getComId(), 0, 0);
		if(result.getCode()!=200){
			return result;
		}

		Map<String ,Object> resMap  =new HashMap<>();
		resMap.put("tagList",result.getData());
		return ResultUtil.success(resMap);
	}

	@PostMapping(WebApiServiceUrl.Url_PerTagAdd)
	public Result addPerTag(@RequestBody EntityPersonTag entityPersonTag) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		entityPersonTag.setComId(yoCiUser.getComId());
		return humanClientService.addPerTag(entityPersonTag);
	}

	@PostMapping(WebApiServiceUrl.Url_PerTagModify)
	public Result PerTagModify(@RequestBody EntityPersonTag entityPersonTag) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		entityPersonTag.setComId(yoCiUser.getComId());
		return humanClientService.updatePerTag(entityPersonTag);
	}

	@PostMapping(WebApiServiceUrl.Url_PerTagGroupOnShow)
	public Result PerTagGroupOnShow() {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		return humanClientService.getPerTagGroup(yoCiUser.getComId());
	}

	@PostMapping(WebApiServiceUrl.Url_PPerTagGroupAdd)
	public Result PerTagGroupAdd(@RequestBody PersonTagTypeRequest request) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		EntityPersonTagType entityPersonTagType = new EntityPersonTagType();

		entityPersonTagType.setComId(yoCiUser.getComId());
		entityPersonTagType.setGroupNo(request.getGroupNo());
		entityPersonTagType.setGroupName(request.getGroupName());
		entityPersonTagType.setRemark(request.getRemark());
		entityPersonTagType.setColor(request.getColor());

		return humanClientService.addPerTagGroup(yoCiUser.getComId(), entityPersonTagType);
	}

	@PostMapping(WebApiServiceUrl.Url_PerTagGroupModify)
	public Result PerTagGroupModify(@RequestBody PersonTagTypeRequest request) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		EntityPersonTagType entityPersonTagType = new EntityPersonTagType();

		entityPersonTagType.setComId(request.getComId());
		entityPersonTagType.setGroupNo(request.getGroupNo());
		entityPersonTagType.setGroupName(request.getGroupName());
		entityPersonTagType.setRemark(request.getRemark());
		entityPersonTagType.setColor(request.getColor());

		return humanClientService.updatePerTagGroup(yoCiUser.getComId(), entityPersonTagType);
	}

	@PostMapping(WebApiServiceUrl.Url_PerTagGroupRemove)
	public Result PerTagGroupRemove(@RequestBody PersonTagTypeRequest request) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();

		RemovePerTagGroupRequest removePerTagGroupRequest = new RemovePerTagGroupRequest();
		removePerTagGroupRequest.setComId(yoCiUser.getComId());
		removePerTagGroupRequest.setGroupNo(request.getGroupNo());
		removePerTagGroupRequest.setType(request.getType());
		removePerTagGroupRequest.setUserName(yoCiUser.getUsername());

		return humanClientService.removePerTagGroup(removePerTagGroupRequest);
	}
}
